//
//  ApprovalFlowHelper.swift
//  ZYA
//
//  Created by 邱仙凯 on 2020/4/21.
//  Copyright © 2020 zwb. All rights reserved.
//

import UIKit

class ApprovalFlowHelper: NSObject {
    
}
protocol approvalFlowable: AnyObject {
    
    func submitNetWork(vc:UIViewController,z_zc_id:String,vou_no:String,voc_cd:String,flow_nm:String,vou_id:String,deptId:String,unitId:String,finish: (() -> Void)?)
    func resubmitNetWork(vc:UIViewController,z_zc_id:String,vou_no:String,voc_cd:String,flow_nm:String,vou_id:String,deptId:String,unitId:String,finish: (() -> Void)?)
}
extension approvalFlowable {
    
    
    
    // MARK: - 提交
    func submitNetWork(vc:UIViewController,z_zc_id:String = "",vou_no:String,voc_cd:String,flow_nm:String,vou_id:String,deptId:String = "",unitId:String = "",finish: (() -> Void)?) {
        
        alertUtil(vc, title: "确认提交", operateS: [nameAction(name: "确定", action: { (_) in
            KSNetwork.shared.doRequest(API.auditFlow(.newSubmit(vou_no: vou_no, voc_cd: voc_cd, flow_nm: flow_nm, vou_id: vou_id,deptId:deptId,unitId:unitId))) { (data:BaseResult<OperateResult>) in
                if data.flag == TRUE {
                    KSToast("成功")
                    self.startNotification(vc:vc,voc_cd: voc_cd,vou_id: vou_id, unit_id: unitId, dept_id: deptId)
                    finish!()
                } else {
                    KSToast(data.errorCombine.unwrap)
                }
            }
      
            
        })])
        
    }
    
    func startNotification(vc:UIViewController,voc_cd:String,vou_id:String,unit_id:String,dept_id:String) {
        KSNetwork.shared.doRequest(API.auditFlow(.getHasNoticeInfo(voc_cd: voc_cd, unit_id: unit_id, dept_id: dept_id))) { (data:BaseResult<AuditNoResult>) in
            if data.flag == TRUE {
                if let info = data.infoObjCombine {
                    if info.is_notice == "1" && info.notice_window == "1" {
                        
                        KSNetwork.shared.doRequest(API.auditFlow(.getNoticeInfo(voc_cd: voc_cd, unit_id: unit_id, dept_id: dept_id))) { (data:BaseResult<AuditStaffResult>) in
                            if data.flag == TRUE {
                                if let infos = data.infoArrCombine {
                                 
                                
                                    let nav = BaseNavigationController()
                                    let vv = AuditSelectManVC()
                                    vv.unit_id = unit_id
                                    vv.voc_cd = voc_cd
                                    vv.vou_id = vou_id
                                    vv.dataArray = infos
                                    nav.viewControllers = [vv]
                                    vc.present_FullScreen(to: nav)
                                    
                               
                                    
                                }
                            } else {
                                KSToast(data.errorCombine.unwrap)
                            }
                        }
                    
                        
                         
                        
                    }
                }
            }
        }
    }
    
    // MARK: - 反提交
    func resubmitNetWork(vc:UIViewController,z_zc_id:String = "",vou_no:String,voc_cd:String,flow_nm:String,vou_id:String,deptId:String = "",unitId:String = "",finish: (() -> Void)?) {
        
        
        alertUtil(vc, title: "确认反提交", operateS: [nameAction(name: "确定", action: { (_) in
            
            
            KSNetwork.shared.doRequest(API.auditFlow(.newResubmit(vou_no: vou_no, voc_cd: voc_cd, flow_nm: flow_nm, vou_id: vou_id,deptId:deptId,unitId:unitId))) { (data:BaseResult<OperateResult>) in
                if data.flag == TRUE {
                    KSToast("成功")
                    finish!()
                } else {
                    KSToast(data.errorCombine.unwrap)
                }
            }
            
            
        })])
        
        
    }
    
}


 


/// 养殖场审批流提交
protocol approvalFlowableOK {
    
    func submitNetWorkOK(vc:UIViewController,z_zc_id:String,vou_no:String,voc_cd:String,flow_nm:String,vou_id:String,unitId:String,finish:(()->())?)
    func resubmitNetWorkOK(vc:UIViewController,z_zc_id:String,vou_no:String,voc_cd:String,flow_nm:String,vou_id:String,unitId:String,finish:(()->())?)
    
}
extension approvalFlowableOK {
    
    func submitNetWorkOK(vc:UIViewController,z_zc_id:String = "",vou_no:String,voc_cd:String,flow_nm:String,vou_id:String,unitId:String,finish:(()->())?) {
        
        
        KSNetworkOK.shared.doRequest(ZHUOKAPI.queryOADeptIdByZcId(z_zc_id: z_zc_id)) { (data:BaseResultOK<OperateResultOK>) in
            if data.flag == TRUE {
                if let info = data.infoObjCombine {
                    
                    let vounoPr = "\(info.oa_dept_id.unwrap)-\(vou_no)"
                    
                    
                    alertUtil(vc, title: "确认提交", operateS: [nameAction(name: "确定", action: { (_) in
                        
                        if checkPlatform(type: .haiXin) {
                            
                            KSNetwork.shared.doRequest(API.auditFlow(.submit(voc_cd: voc_cd, vouid: vou_id, vou_id: vou_id, dept_id: info.oa_dept_id.unwrap, org_code: USERINFO.orgCode.unwrap))) { (data:BaseResult<OperateResult>) in
                                if data.flag == TRUE {
                                    KSToast("成功")
                                    finish?()
                                } else {
                                    KSToast(data.errorCombine.unwrap)
                                }
                            }
                        } else {
                            KSNetworkOK.shared.doRequest(  ZHUOKAPI.newSubmit(vou_no: vounoPr, voc_cd: voc_cd, flow_nm: flow_nm, vou_id: vou_id,deptId:info.oa_dept_id.unwrap,unitId:unitId)) { (data:BaseResultOK<OperateResultOK>) in
                                if data.flag == TRUE {
                                    KSToast("成功")
                                    finish!()
                                } else {
                                    KSToast(data.errorCombine.unwrap)
                                }
                            }
                        }

                    })])
    
                    
                }
                
            } else {
                KSToast(data.errorCombine.unwrap)
            }
        }
        
        
        
    }
    func resubmitNetWorkOK(vc:UIViewController,z_zc_id:String = "",vou_no:String,voc_cd:String,flow_nm:String,vou_id:String,unitId:String,finish:(()->())?) {
        
        
        KSNetworkOK.shared.doRequest(ZHUOKAPI.queryOADeptIdByZcId(z_zc_id: z_zc_id)) { (data:BaseResultOK<OperateResultOK>) in
            if data.flag == TRUE {
                if let info = data.infoObjCombine {
                    
                    let vounoPr = "\(info.oa_dept_id.unwrap)-\(vou_no)"
                    
                    
                    alertUtil(vc, title: "确认反提交", operateS: [nameAction(name: "确定", action: { (_) in
                        if checkPlatform(type: .haiXin) {
                            KSNetwork.shared.doRequest(API.auditFlow(.cancelSubmit(comment: "", voc_cd: voc_cd, vouid: vou_id, vou_id: vou_id))) { (data:BaseResult<OperateResult>) in
                                if data.flag == TRUE {
                                    KSToast("成功")
                                    finish?()
                                } else {
                                    KSToast(data.errorCombine.unwrap)
                                }
                            }
                            
                        } else {
                            KSNetworkOK.shared.doRequest(ZHUOKAPI.newResubmit(vou_no: vounoPr, voc_cd: voc_cd, flow_nm: flow_nm, vou_id: vou_id,deptId:info.oa_dept_id.unwrap,unitId:unitId)) { (data:BaseResultOK<OperateResultOK>) in
                                if data.flag == TRUE {
                                    KSToast("成功")
                                    finish!()
                                } else {
                                    KSToast(data.errorCombine.unwrap)
                                }
                            }
                        }
                        
                    })])
                    
                    
                    
            
                    
                }
                
            } else {
                KSToast(data.errorCombine.unwrap)
            }
        }
        
        
        
    }
}

class AuditSelectManVC: UITableViewController {
    
    var dataArray = [AuditStaffResult]()
    var voc_cd = ""
    var vou_id = ""
     var unit_id  = ""
    var backClosure:((Discounts)->())?
    override func viewDidLoad() {
        super.viewDidLoad()
      
         self.title = "当前知会人员名单"
     
        
        let leftBtn = UIButton.creat4Item(title: "返回") { _ in
            self.dismiss(animated: true)
        }
        let left = UIBarButtonItem(customView: leftBtn)
       
        self.navigationItem.leftBarButtonItem = left
        
        let rightBtn = UIButton.creat4Item(title: "保存") { _ in
            self.saveAction()
        }
        let right = UIBarButtonItem(customView: rightBtn)
       
        self.navigationItem.rightBarButtonItem = right
      
        view.backgroundColor = Theme.BACKGROUND_COLOR
 
        self.tableView.separatorStyle = .none
        self.tableView.register(AuditSelectManCell.self, forCellReuseIdentifier: AuditSelectManCell.identifier)
        self.tableView.rowHeight = 50
        self.tableView.backgroundColor = #colorLiteral(red: 0.9607843137, green: 0.9607843137, blue: 0.9607843137, alpha: 1)
        
      
        let v = UIView(frame: CGRect(x: 6, y: 0, width: KSGlobal.screenW - 12, height: 41))
        v.backgroundColor = .clear
        let footer = UIButton(frame: CGRect(x: 0, y: 6, width: KSGlobal.screenW - 12, height: 35))
        footer.backgroundColor = .white
        footer.setTitleColor(Theme.primaryColor(), for: .normal)
        footer.layer.cornerRadius = 8
        footer.setTitle("新增", for: .normal)
        footer.addTouchUpInSideAction { _ in
         
            KSNetwork.shared.doRequest(API.auditFlow(.selectNoticeMan(unit_id:self.unit_id))) { (data:BaseResult<AuditStaffResult>) in
                if data.flag == TRUE {
                    if let infos = data.infoArrCombine {
                     
                     
                        var allArr = [(AuditStaffResult,Bool)]()
                        for i in 0..<infos.count {
                             allArr.append((infos[i],false))
                        }
                        let cle:([Any])->() = { arr in
                            
                            for item in arr {
                                var onex = item as! AuditStaffResult
                                onex.staff_id = onex.id_key
                                onex.staff_name = onex.c_name
                                onex.defaultx = false
                                self.dataArray.append(onex)
                            }
                            self.tableView.reloadData()
                        }
                        let showC:((GlobalTableViewCell, Any) -> ()) = { cell, model in
                            var contArr:[(String,String?)]!
                            contArr = [
                                       ("","\((model as! AuditStaffResult).c_name.unwrap)")
                                    ]
                           cell.setDataSource(contentArr: contArr)
                        }
                        let filterC:((Any, String) -> (Bool)) = { model, keyword -> Bool in
                            return (model as! AuditStaffResult).c_name.unwrap.contains(keyword)
                        }
                         let vc = KSMultiSelectUtil.initWithNav(title: "选择", allArr: allArr, backClosure: cle,kSShowCell: showC , filterClosure: filterC)
                        let topViewController = RouterUtils.currentTopViewController()
                        topViewController!.present_FullScreen(to: vc)
                
                       
                        
                    }
                } else {
                    KSToast(data.errorCombine.unwrap)
                }
            }
            
            
            
            
            
        }
        v.addSubview(footer)
        
        self.tableView.tableFooterView = v
 
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   
    }
 
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: AuditSelectManCell.identifier, for: indexPath) as? AuditSelectManCell else {
            fatalError()
        }
        let one = self.dataArray[indexPath.row]
        cell.left1.text = one.staff_name
        cell.deleteBtn.isHidden = one.defaultx
        cell.deleteClosure = { [indexPath] in
            alertUtil(self, title: "确认删除", operateS: [nameAction(name: "确定", action: { _ in
                self.dataArray.remove(at: indexPath.row)
                self.tableView.reloadData()
            })])
        }
     
 
        return cell
        
    }

 
    func saveAction() {
        guard self.dataArray.count > 0 else {
            KSToast("未选择人员")
            return
        }
        let joinS = self.dataArray.compactMap{$0.staff_id}.joined(separator: ",")
        KSNetwork.shared.doRequest(API.auditFlow(.saveVouNoticeInfo(voc_cd: voc_cd, vou_id: vou_id, staff_id: USERINFO.staffId.unwrap, data: joinS))) { (data:BaseResult<AuditStaffResult>) in
            if data.flag == TRUE {
            KSToast("成功")
                self.dismiss(animated: true)
            } else {
                KSToast(data.errorCombine.unwrap)
            }
        }
        
        
 
    }
}

class AuditSelectManCell: GlobalTableViewCell {
    lazy var left1:UILabel = {
       let v = UILabel()
        v.text = "直返金额"
        return v
    }()
//    lazy var left2:UILabel = {
//       let v = UILabel()
//        v.text = "产品名称"
//        return v
//    }()
 
    lazy var goaheadBtn:FenJBtn = {
       let v = FenJBtn()
        v.titleLabel?.textAlignment = .right
        v.setTitleColor(.black, for: .normal)
           return v
    }()
 
    lazy var deleteBtn:UIButton = {
       let v = UIButton()
        v.setBackgroundImage(UIImage(named:"lajitong"), for: .normal)
          return v
    }()
    var deleteClosure:(()->())?
    var goodsClosure:(()->())?

    var textClosure:((String)->())?

    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
    
        bg.addSubview(left1)
//        bg.addSubview(left2)
      
        bg.addSubview(goaheadBtn)
        goaheadBtn.addTouchUpInSideAction { _ in
            if let clo = self.goodsClosure {
                clo()
            }
        }
        
         bg.addSubview(deleteBtn)
        deleteBtn.addTouchUpInSideAction { _ in
            if let clo = self.deleteClosure {
                clo()
            }
        }
        
        left1.snp.makeConstraints { make in
            make.left.equalTo(bg).offset(10)
            make.centerY.equalTo(bg)
        }
//        left2.snp.makeConstraints { make in
//            make.left.equalTo(self.left1)
//            make.bottom.equalTo(bg).offset(-12)
//        }
    
        deleteBtn.snp.makeConstraints { make in
            make.right.equalTo(bg).offset(-15)
            make.width.equalTo(28)
            make.height.equalTo(28)
            make.centerY.equalTo(bg)
        }
    }
 
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        if let clo = self.textClosure {
            clo(textField.text.unwrap)
        }
    }
}
class FenJBtn:UIButton {
    override func layoutSubviews() {
        super.layoutSubviews()
        self.titleLabel?.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
    }
}

import ObjectMapper
class AuditNoResult:Mappable {
    var notice_window:String?
    var is_notice:String?
    
     required init?(map: Map) {
        
    }
    // Mappable
    func mapping(map: Map) {
      
        notice_window <- (map["notice_window"],StringTransform())
        
   
        is_notice <- (map["is_notice"],StringTransform())
      
    }
}
class AuditStaffResult:Mappable {
    var role_name:String?
    var staff_id:String?
    var staff_name:String?
    var id_key:String?
    var c_name:String?
   var defaultx = true
     required init?(map: Map) {
        
    }
    // Mappable
    func mapping(map: Map) {
      
        staff_name <- (map["staff_name"],StringTransform())
        role_name <- (map["role_name"],StringTransform())
        staff_id <- (map["staff_id"],StringTransform())
        id_key <- (map["id_key"],StringTransform())
        c_name <- (map["c_name"],StringTransform())
    }
}

open class Discounts: SwiftyJSONAble {

    // MARK: Declaration for string constants to be used to decode and also serialize.
    internal let kDiscountsSSalesmanNmKey: String = "s_salesman_nm"
    internal let kDiscountsSAreaNmKey: String = "s_area_nm"
    internal let kDiscountsSAreaManagerIdKey: String = "s_area_manager_id"
    internal let kDiscountsSMonthZkbsKey: String = "s_month_zkbs"
    internal let kDiscountsSEndDtKey: String = "s_end_dt"
    internal let kDiscountsSNoKey: String = "s_no"
    internal let kDiscountsSZsIdKey: String = "s_zs_id"
    internal let kDiscountsSZsId5Key: String = "s_zs_id5"
    internal let kDiscountsSZsId3Key: String = "s_zs_id3"
    internal let kDiscountsSZsId4Key: String = "s_zs_id4"
    internal let kDiscountsSZsId2Key: String = "s_zs_id2"
    internal let kDiscountsAuditMarkKey: String = "audit_mark"
    internal let kDiscountsSMonthZkjeKey: String = "s_month_zkje"
    internal let kDiscountsSBigTypeKey: String = "s_big_type"
    internal let kDiscountsSBigTypeNmKey: String = "s_big_type_nm"
    internal let kDiscountsSBeginDtKey: String = "s_begin_dt"
    internal let kDiscountsSYearKey: String = "s_year"
    internal let kDiscountsSZsNmKey: String = "s_zs_nm"
    internal let kDiscountsRnKey: String = "rn"
    internal let kDiscountsSTypeKey: String = "s_type"
    internal let kDiscountsMakeDtKey: String = "make_dt"
    internal let kDiscountsMakeIdKey: String = "make_id"
    internal let kDiscountsSClientNmKey: String = "s_client_nm"
    internal let kDiscountsSSalesmanIdKey: String = "s_salesman_id"
    internal let kDiscountsSRemarkKey: String = "s_remark"
    internal let kDiscountsCwAuditMarkKey: String = "cw_audit_mark"
    internal let kDiscountsMakeNmKey: String = "make_nm"
    internal let kDiscountsSContentKey: String = "s_content"
    internal let kDiscountsIdKeyKey: String = "id_key"
    internal let kDiscountsSClientIdKey: String = "s_client_id"
    internal let kDiscountsSMonthZkbs2Key: String = "s_month_zkbs2"
    internal let kDiscountsSZsNm2Key: String = "s_zs_nm2"
    internal let kDiscountsSMonthZkbs4Key: String = "s_month_zkbs4"
    internal let kDiscountsSZsNm4Key: String = "s_zs_nm4"
    internal let kDiscountsSMonthZkbs3Key: String = "s_month_zkbs3"
    internal let kDiscountsSZsNm3Key: String = "s_zs_nm3"
    internal let kDiscountsSMonthZkbs5Key: String = "s_month_zkbs5"
    internal let kDiscountsSZsNm5Key: String = "s_zs_nm5"
    internal let kDiscountsSAreaIdKey: String = "s_area_id"
    internal let kDiscountsOrgCodeKey: String = "org_code"
    internal let kDiscountsOrgNameKey: String = "org_name"
    internal let kDiscountsCwAuditMarkNmKey: String = "cw_audit_mark_nm"
    internal let kDiscountsAuditMarkNmKey: String = "audit_mark_nm"
    internal let kDiscountsContractNoKey: String = "s_contract_no"
    internal let zkDetailKey = "zk_detail"
    internal let zifycKey = "if_yc"
    internal let sMonthKey = "s_month"
    internal let overproofKey = "overproof"
    internal let operationQrKey = "operation_qr"

 
    // MARK: Properties
    open var sSalesmanNm: String?
    open var sAreaNm: String?
    open var sAreaId: String?
    open var sAreaManagerId: String?
    open var sMonthZkbs: String?
    open var sEndDt: String?
    open var sNo: String?
    open var sZsId: String?
    open var sZsId5: String?
    open var sZsId3: String?
    open var sZsId4: String?
    open var sZsId2: String?
    open var auditMark: String?
    open var sMonthZkje: String?
    open var sBigType: String?
    open var sBigTypeNm: String?
    open var sBeginDt: String?
    open var sYear: String?
    open var sZsNm: String?
    open var rn: String?
    open var sType: String?
    open var makeDt: String?
    open var makeId: String?
    open var sClientNm: String?
    open var sSalesmanId: String?
    open var sRemark: String?
    open var cwAuditMark: String?
    open var makeNm: String?
    open var sContent: String?
    open var idKey: String?
    open var sClientId: String?
    open var sMonthZkbs2: String?
    open var sZsNm2: String?
    open var sMonthZkbs3: String?
    open var sZsNm3: String?
    open var sMonthZkbs4: String?
    open var sZsNm4: String?
    open var sMonthZkbs5: String?
    open var sZsNm5: String?
    open var orgCode: String?
    open var orgName: String?
    open var auditMarkNm: String?
    open var cwAuditMarkNm: String?
    open var sContractNo: String?
    open var ifYc: String?
    open var sMonth: String?
    open var overproof: String?
    open var operationQr: String?

    
    
    open var isOpen = false
    open var pictrues = [ComPictrue]()
    open var height: CGFloat = 0
    public var zkDetail = [ZkDetail]()

    // MARK: SwiftyJSON Initalizers
    /**
    Initates the class based on the object
    - parameter object: The object of either Dictionary or Array kind that was passed.
    - returns: An initalized instance of the class.
    */
    convenience public init(object: Any) {
        self.init(json: JSON(object))
    }

    /**
    Initates the class based on the JSON that was passed.
    - parameter json: JSON object from SwiftyJSON.
    - returns: An initalized instance of the class.
    */
    public required init(json: JSON) {
        sSalesmanNm = json[kDiscountsSSalesmanNmKey].string
        sAreaNm = json[kDiscountsSAreaNmKey].string
        sAreaManagerId = json[kDiscountsSAreaManagerIdKey].string
        sMonthZkbs = json[kDiscountsSMonthZkbsKey].stringValue
        sEndDt = json[kDiscountsSEndDtKey].string
        sNo = json[kDiscountsSNoKey].string
        sZsId = json[kDiscountsSZsIdKey].stringValue
        sZsId5 = json[kDiscountsSZsId5Key].stringValue
        sZsId3 = json[kDiscountsSZsId3Key].stringValue
        sZsId4 = json[kDiscountsSZsId4Key].stringValue
        sZsId2 = json[kDiscountsSZsId2Key].stringValue
        auditMark = json[kDiscountsAuditMarkKey].stringValue
        sMonthZkje = json[kDiscountsSMonthZkjeKey].stringValue
        sBigType = json[kDiscountsSBigTypeKey].stringValue
        sBigTypeNm = json[kDiscountsSBigTypeNmKey].string
        sBeginDt = json[kDiscountsSBeginDtKey].string
        sYear = json[kDiscountsSYearKey].string
        sZsNm = json[kDiscountsSZsNmKey].string
        rn = json[kDiscountsRnKey].stringValue
        sType = json[kDiscountsSTypeKey].stringValue
        makeDt = json[kDiscountsMakeDtKey].string
        makeId = json[kDiscountsMakeIdKey].stringValue
        auditMark = json[kDiscountsAuditMarkKey].stringValue
        sClientNm = json[kDiscountsSClientNmKey].string
        sSalesmanId = json[kDiscountsSSalesmanIdKey].stringValue
        sRemark = json[kDiscountsSRemarkKey].string
        cwAuditMark = json[kDiscountsCwAuditMarkKey].stringValue
        makeNm = json[kDiscountsMakeNmKey].string
        sContent = json[kDiscountsSContentKey].string
        idKey = json[kDiscountsIdKeyKey].stringValue
        sClientId = json[kDiscountsSClientIdKey].stringValue
        cwAuditMarkNm = json[kDiscountsCwAuditMarkNmKey].stringValue
        sMonthZkbs2 = json[kDiscountsSMonthZkbs2Key].stringValue
        sZsNm2 = json[kDiscountsSZsNm2Key].string
        sMonthZkbs3 = json[kDiscountsSMonthZkbs3Key].stringValue
        sZsNm3 = json[kDiscountsSZsNm3Key].string
        sMonthZkbs4 = json[kDiscountsSMonthZkbs4Key].stringValue
        sZsNm4 = json[kDiscountsSZsNm4Key].string
        sMonthZkbs5 = json[kDiscountsSMonthZkbs5Key].stringValue
        sZsNm5 = json[kDiscountsSZsNm5Key].string
        sAreaId = json[kDiscountsSAreaIdKey].int.isSome ? "\(json[kDiscountsSAreaIdKey].int!)" : json[kDiscountsSAreaIdKey].string
        orgCode = json[kDiscountsOrgCodeKey].int.isSome ? "\(json[kDiscountsOrgCodeKey].int!)" : json[kDiscountsOrgCodeKey].string
        orgName = json[kDiscountsOrgNameKey].stringValue
        auditMarkNm = json[kDiscountsAuditMarkNmKey].string
        sContractNo = json[kDiscountsContractNoKey].stringValue
        ifYc = json[zifycKey].int.isSome ? "\(json[zifycKey].int!)" : json[zifycKey].string
        sMonth = json[sMonthKey].string
        overproof = json[overproofKey].string
        operationQr = json[operationQrKey].string

        
        
        if let items = json[zkDetailKey].array { zkDetail = items.map { ZkDetail(json: $0) } }

    }

    /**
    Generates description of the object in the form of a NSDictionary.
    - returns: A Key value pair containing all valid values in the object.
    */
    open func dictionaryRepresentation() -> [String : Any] {

        var dictionary: [String : Any] = [ : ]
        if sSalesmanNm != nil {
            dictionary.updateValue(sSalesmanNm!, forKey: kDiscountsSSalesmanNmKey)
        }
        if sAreaNm != nil {
            dictionary.updateValue(sAreaNm!, forKey: kDiscountsSAreaNmKey)
        }
        if sAreaManagerId != nil {
            dictionary.updateValue(sAreaManagerId!, forKey: kDiscountsSAreaManagerIdKey)
        }
        if sMonthZkbs != nil {
            dictionary.updateValue(sMonthZkbs!, forKey: kDiscountsSMonthZkbsKey)
        }
        if sEndDt != nil {
            dictionary.updateValue(sEndDt!, forKey: kDiscountsSEndDtKey)
        }
        if sNo != nil {
            dictionary.updateValue(sNo!, forKey: kDiscountsSNoKey)
        }
        if sZsId != nil {
            dictionary.updateValue(sZsId!, forKey: kDiscountsSZsIdKey)
        }

        if let value = sZsId5 { dictionary[kDiscountsSZsId5Key] = value }
        if let value = sZsId3 { dictionary[kDiscountsSZsId3Key] = value }
        if let value = sZsId4 { dictionary[kDiscountsSZsId4Key] = value }
        if let value = sZsId2 { dictionary[kDiscountsSZsId2Key] = value }

        if auditMark != nil {
            dictionary.updateValue(auditMark!, forKey: kDiscountsAuditMarkKey)
        }
        if sMonthZkje != nil {
            dictionary.updateValue(sMonthZkje!, forKey: kDiscountsSMonthZkjeKey)
        }
        if sBigType != nil {
            dictionary.updateValue(sBigType!, forKey: kDiscountsSBigTypeKey)
        }
        if sBigTypeNm != nil {
            dictionary.updateValue(sBigTypeNm!, forKey: kDiscountsSBigTypeNmKey)
        }
        if sBeginDt != nil {
            dictionary.updateValue(sBeginDt!, forKey: kDiscountsSBeginDtKey)
        }
        if sYear != nil {
            dictionary.updateValue(sYear!, forKey: kDiscountsSYearKey)
        }
        if sZsNm != nil {
            dictionary.updateValue(sZsNm!, forKey: kDiscountsSZsNmKey)
        }
        if rn != nil {
            dictionary.updateValue(rn!, forKey: kDiscountsRnKey)
        }
        if sType != nil {
            dictionary.updateValue(sType!, forKey: kDiscountsSTypeKey)
        }
        if makeDt != nil {
            dictionary.updateValue(makeDt!, forKey: kDiscountsMakeDtKey)
        }
        if makeId != nil {
            dictionary.updateValue(makeId!, forKey: kDiscountsMakeIdKey)
        }
        if auditMarkNm != nil {
            dictionary.updateValue(auditMarkNm!, forKey: kDiscountsAuditMarkNmKey)
        }
        if sClientNm != nil {
            dictionary.updateValue(sClientNm!, forKey: kDiscountsSClientNmKey)
        }
        if sSalesmanId != nil {
            dictionary.updateValue(sSalesmanId!, forKey: kDiscountsSSalesmanIdKey)
        }
        if sRemark != nil {
            dictionary.updateValue(sRemark!, forKey: kDiscountsSRemarkKey)
        }
        if cwAuditMark != nil {
            dictionary.updateValue(cwAuditMark!, forKey: kDiscountsCwAuditMarkKey)
        }
        if makeNm != nil {
            dictionary.updateValue(makeNm!, forKey: kDiscountsMakeNmKey)
        }
        if sContent != nil {
            dictionary.updateValue(sContent!, forKey: kDiscountsSContentKey)
        }
        if idKey != nil {
            dictionary.updateValue(idKey!, forKey: kDiscountsIdKeyKey)
        }
        if sClientId != nil {
            dictionary.updateValue(sClientId!, forKey: kDiscountsSClientIdKey)
        }
        if cwAuditMarkNm != nil {
            dictionary.updateValue(cwAuditMarkNm!, forKey: kDiscountsCwAuditMarkNmKey)
        }

        if sMonthZkbs2 != nil {
            dictionary.updateValue(sMonthZkbs2!, forKey: kDiscountsSMonthZkbs2Key)
        }
        if sZsNm2 != nil {
            dictionary.updateValue(sZsNm2!, forKey: kDiscountsSZsNm2Key)
        }
        if sMonthZkbs3 != nil {
            dictionary.updateValue(sMonthZkbs3!, forKey: kDiscountsSMonthZkbs3Key)
        }
        if sZsNm3 != nil {
            dictionary.updateValue(sZsNm3!, forKey: kDiscountsSZsNm3Key)
        }
        if sMonthZkbs4 != nil {
            dictionary.updateValue(sMonthZkbs4!, forKey: kDiscountsSMonthZkbs4Key)
        }
        if sZsNm4 != nil {
            dictionary.updateValue(sZsNm4!, forKey: kDiscountsSZsNm4Key)
        }
        if sMonthZkbs5 != nil {
            dictionary.updateValue(sMonthZkbs5!, forKey: kDiscountsSMonthZkbs5Key)
        }
        if sZsNm5 != nil {
            dictionary.updateValue(sZsNm5!, forKey: kDiscountsSZsNm5Key)
        }
        if sAreaId != nil {
            dictionary.updateValue(sAreaId!, forKey: kDiscountsSAreaIdKey)
        }

        if let value = orgName { dictionary[kDiscountsOrgNameKey] = value }
        if let value = orgCode { dictionary[kDiscountsOrgCodeKey] = value }

        return dictionary
    }

}
import SwiftyJSON

public final class ZkDetail {

  // MARK: Declaration for string constants to be used to decode and also serialize.
  private struct SerializationKeys {
    static let goodsNm = "goods_nm"
    static let idKey = "id_key"
    static let vouId = "vou_id"
    static let zfMoney = "zf_money"
    static let goodsId = "goods_id"
  }

  // MARK: Properties
  public var goodsNm: String?
  public var idKey: Int?
  public var vouId: Int?
  public var zfMoney: Double?
  public var goodsId: String?

  // MARK: SwiftyJSON Initializers
  /// Initiates the instance based on the object.
  ///
  /// - parameter object: The object of either Dictionary or Array kind that was passed.
  /// - returns: An initialized instance of the class.
  public convenience init(object: Any) {
    self.init(json: JSON(object))
  }

  /// Initiates the instance based on the JSON that was passed.
  ///
  /// - parameter json: JSON object from SwiftyJSON.
  public required init(json: JSON) {
    goodsNm = json[SerializationKeys.goodsNm].string
    idKey = json[SerializationKeys.idKey].int
    vouId = json[SerializationKeys.vouId].int
    zfMoney = json[SerializationKeys.zfMoney].double
    goodsId = json[SerializationKeys.goodsId].int.isSome ? "\(json[SerializationKeys.goodsId].int!)" : json[SerializationKeys.goodsId].string
  }

  /// Generates description of the object in the form of a NSDictionary.
  ///
  /// - returns: A Key value pair containing all valid values in the object.
  public func dictionaryRepresentation() -> [String: Any] {
    var dictionary: [String: Any] = [:]
    if let value = goodsNm { dictionary[SerializationKeys.goodsNm] = value }
    if let value = idKey { dictionary[SerializationKeys.idKey] = value }
    if let value = vouId { dictionary[SerializationKeys.vouId] = value }
    if let value = zfMoney { dictionary[SerializationKeys.zfMoney] = value }
    if let value = goodsId { dictionary[SerializationKeys.goodsId] = value }
    return dictionary
  }
 

}
open class ComPictrue: SwiftyJSONAble {
    private struct SerializationKeys {
      static let s_pic_url = "s_pic_url"
        static let c_file_url = "c_file_url"
      static let id_key = "id_key"
      static let s_pic_type = "s_pic_type"
      static let c_vou_id = "c_vou_id"
      static let vou_id = "vou_id"
        static let s_pic_byte = "s_pic_byte"

    }
 
  // MARK: Properties
  open var sPicUrl: String?
  open var idKey: String?
  open var sPicType: String?
    open var sPicByte: String?
  open var vouId: String?
    open var image:UIImage?
  // MARK: SwiftyJSON Initalizers
  /**
   Initates the instance based on the object
   - parameter object: The object of either Dictionary or Array kind that was passed.
   - returns: An initalized instance of the class.
  */
  convenience public init(object: Any) {
    self.init(json: JSON(object))
  }

  /**
   Initates the instance based on the JSON that was passed.
   - parameter json: JSON object from SwiftyJSON.
   - returns: An initalized instance of the class.
  */
  public required init(json: JSON) {
      sPicUrl = json[SerializationKeys.s_pic_url].string.isSome ? json[SerializationKeys.s_pic_url].string : json[SerializationKeys.c_file_url].string
      
      idKey = json[SerializationKeys.id_key].int.isSome ? json[SerializationKeys.id_key].int.unwrap : json[SerializationKeys.id_key].string
      
      let vou = json[SerializationKeys.vou_id].int.isSome ? json[SerializationKeys.vou_id].int.unwrap : json[SerializationKeys.vou_id].string
      let cvou = json[SerializationKeys.c_vou_id].int.isSome ? json[SerializationKeys.c_vou_id].int.unwrap : json[SerializationKeys.c_vou_id].string
      vouId = vou.isSome ? vou : cvou
      
      sPicType = json[SerializationKeys.s_pic_type].stringValue
      sPicByte = json[SerializationKeys.s_pic_byte].stringValue
  }

  /**
   Generates description of the object in the form of a NSDictionary.
   - returns: A Key value pair containing all valid values in the object.
  */
  open func dictionaryRepresentation() -> [String: Any] {
    var dictionary: [String: Any] = [:]
      if let value = sPicUrl {
          dictionary[SerializationKeys.s_pic_url] = value
          dictionary[SerializationKeys.c_file_url] = value
      }
      if let value = idKey { dictionary[SerializationKeys.id_key] = value } else {
          dictionary[SerializationKeys.id_key] = ""
    }
    if let value = sPicType { dictionary[SerializationKeys.s_pic_type] = value }
      if let value = vouId {
          dictionary[SerializationKeys.vou_id] = value
          dictionary[SerializationKeys.c_vou_id] = ""
      } else {
          dictionary[SerializationKeys.vou_id] = ""
          dictionary[SerializationKeys.c_vou_id] = ""
    }
      if let value = sPicByte { dictionary[SerializationKeys.s_pic_byte] = value }
    return dictionary
  }

}
